package com.stud.system.service.impl;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import com.stud.common.exception.ServiceException;
import com.stud.common.utils.StringUtils;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.stud.common.core.domain.entity.SysDictData;
import com.stud.common.utils.DictUtils;
import com.stud.system.mapper.SysDictDataMapper;
import com.stud.system.service.SysDictDataService;
import org.springframework.util.CollectionUtils;

/**
 * 字典 业务层处理
 *
 * @author Stud
 */
@Service
public class SysDictDataServiceImpl implements SysDictDataService {
    @Autowired
    private SysDictDataMapper dictDataMapper;

    /**
     * 根据条件分页查询字典数据
     *
     * @param dictData 字典数据信息
     * @return 字典数据集合信息
     */
    @Override
    public List<SysDictData> selectDictDataList(SysDictData dictData) {
        return dictDataMapper.selectDictDataList(dictData);
    }

    /**
     * 根据字典类型和字典键值查询字典数据信息
     *
     * @param dictType  字典类型
     * @param dictValue 字典键值
     * @return 字典标签
     */
    @Override
    public String selectDictLabel(String dictType, String dictValue) {
        return dictDataMapper.selectDictLabel(dictType, dictValue);
    }

    @Override
    public SysDictData getDictData(String dictType, String dictValue) {
        SysDictData dictData = dictDataMapper.getDictData(dictType, dictValue);
        if (Objects.isNull(dictData)) {
            dictData = new SysDictData();
        }
        return dictData;
    }


    /**
     * 根据字典类型和字典键值查询字典数据信息
     *
     * @param dictType 字典类型
     * @return remark 备注
     */
    @Override
    public String getDictRemark(String dictType, String dictValue) {
        return dictDataMapper.selectDictRemark(dictType, dictValue);
    }


    /**
     * 根据字典数据ID查询信息
     *
     * @param dictCode 字典数据ID
     * @return 字典数据
     */
    @Override
    public SysDictData selectDictDataById(Long dictCode) {
        return dictDataMapper.selectDictDataById(dictCode);
    }

    /**
     * 批量删除字典数据信息
     *
     * @param dictCodes 需要删除的字典数据ID
     */
    @Override
    public void deleteDictDataByIds(Long[] dictCodes) {
        for (Long dictCode : dictCodes) {
            SysDictData data = selectDictDataById(dictCode);
            dictDataMapper.deleteDictDataById(dictCode);
            List<SysDictData> dictDatas = dictDataMapper.selectDictDataByType(data.getDictType());
            DictUtils.setDictCache(data.getDictType(), dictDatas);
        }
    }

    /**
     * 新增保存字典数据信息
     *
     * @param data 字典数据信息
     * @return 结果
     */
    @Override
    public int insertDictData(SysDictData data) {
        int row = dictDataMapper.insertDictData(data);
        if (row > 0) {
            List<SysDictData> dictDatas = dictDataMapper.selectDictDataByType(data.getDictType());
            DictUtils.setDictCache(data.getDictType(), dictDatas);
        }
        return row;
    }

    /**
     * 修改保存字典数据信息
     *
     * @param data 字典数据信息
     * @return 结果
     */
    @Override
    public int updateDictData(SysDictData data) {
        int row = dictDataMapper.updateDictData(data);
        if (row > 0) {
            List<SysDictData> dictDatas = dictDataMapper.selectDictDataByType(data.getDictType());
            DictUtils.setDictCache(data.getDictType(), dictDatas);
        }
        return row;
    }

    @Override
    public Map<String, String> dictMap(String dictType, String... dictValues) {
        List<SysDictData> list = this.dictList(dictType);

        if (CollectionUtils.isEmpty(list)) {
            return Maps.newHashMap();
        }

        Map<String, String> map = list.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));

        return map;
    }

    @Override
    public List<SysDictData> dictList(String dictType, String... dictValues) {
        SysDictData dictData = new SysDictData();
        dictData.setDictType(dictType);
        dictData.setDictValues(dictValues);
        return this.selectDictDataList(dictData);
    }

    @Override
    public List<String> getDictValueByDictType(String dictType) {
        if (StringUtils.isBlank(dictType)) {
            throw new ServiceException("字典类型不能为空");
        }
        //查询这个字典类型是否存在
        Integer count = dictDataMapper.existsDictType(dictType);
        if (count == 0) {
            throw new ServiceException("没有该字典类型");
        }
        return dictDataMapper.selectDictValueByDictType(dictType);
    }
}
